﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class enemy : npc
{
    [SerializeField]
    private LootTable lootTable;
    public float attackCooldown;
    [SerializeField]
    private CanvasGroup TRGTgrp;
    [SerializeField]
    private CanvasGroup HPgrp;
    public Vector3 MyStartPosition { get; set; }
    private Istate currentState;
    
    private void Awake()
    {
        changeState(new IdleState());
        MyStartPosition = transform.position;
    }

    protected override void Update()
    {
        
        if (IsAlive)
        {
           
            if (!IsAttacking)
            {
                AttackTime += Time.deltaTime;
                
            }
            currentState.update();
        }
       
        base.Update();
        
    }
    public override void Deselect()
    {

        TRGTgrp.alpha = 0;
        HPgrp.alpha = 0;
        base.Deselect();
    }
    public override Transform Select()
    {
        if(HPbarShow == true)
        {
            HPgrp.alpha = 1;
            TRGTgrp.alpha = 0;
        }
        else
        {
            TRGTgrp.alpha = 1;
            HPgrp.alpha = 0;
        }
       
        return base.Select();
    }


   public void changeState(Istate newState)
    {
        if (currentState != null)
        {
            currentState.exit();
            
        }
        currentState = newState;
        currentState.enter(this);
    }
    public override void Interact()
    {
        
        if (!IsAlive)
        {
            //lootTable.ShowLoot();
            List<Item> loot=new List<Item>();
            foreach(IInteractable tempi in Player.MyInstance.MyInteractables)
            {
                if(tempi is enemy && !(tempi as enemy).IsAlive) 
                {
                    
                }
            }
        }
        
    }
    public override void StopInteract()
    {
        
        LootWindow.MyInstance.Close();
    }
    public void Attack()
    {
        
        Player.MyInstance.ReceiveDMG(dmg, transform);
       
    }
    public override void ReceiveDMG(float dmg, Transform source)
    {
        if(!(currentState is EvadeState))
        {
            base.ReceiveDMG(dmg, source);
        }
        
    }
}
